package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Train;
import com.easy.bean.User;
import com.easy.mapper.TrainMapper;
import com.easy.service.TrainService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainServiceImpl implements TrainService {

    @Autowired
    TrainMapper trainMapper;


    @Override
    @LogMethod(serviceName = "添加列车", recordParam = true, recordReturn = true)
    public Train add(Train train) {
        trainMapper.add(train);
        Train result = trainMapper.one(train.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除列车", recordParam = true, recordReturn = true)
    @CacheEvict(value = "train", key = "#p0" ,allEntries = true)//清空value制定的所有实例
    public int del(Integer id) {
        return trainMapper.del(id);
    }

    @Override
    @LogMethod(serviceName = "修改列车", recordParam = true, recordReturn = true)
    @CachePut(value = "train", key = "#p0.id")
    public Train edit(Train train) {
        trainMapper.edit(train);
        Train result = trainMapper.one(train.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "列车查询", recordParam = true, recordReturn = true)
    @Cacheable(value = "train", key = "#p0")//也可以#p0表示第一个参数
    public Train one(Integer id) {
        return trainMapper.one(id);
    }

    @Override
    public List<Train> all(Train train) {
        return trainMapper.all(train);
    }

    @Override
    public void page(Page page, Train train) {
        int count = trainMapper.pageCount(train);
        Object data = trainMapper.page(page,train);
        page.setCount(count);
        page.setData(data);
    }
}
